<template>
  <component :is="view" :data="data"></component>
</template>

<script>
import CompMain from './main.vue'
import CompTest from './test.vue'
import CompLogin from './login.vue'
import CompReg from './reg.vue'
import CompHome from './home.vue'
import CompBlogAdd from './publish/add.vue'
import CompBlogDetail from './publish/detail.vue'
export default {
  data () {
    return {
      currentView:'comp-main',
      data:{},
      history:[{compName:'comp-main'}],
      currentIndex:0
    }
  },
  computed:{
    view(){
      return this.currentView
    }
  },
  components:{
    CompTest,
    CompMain,
    CompLogin,
    CompReg,
    CompHome,
    CompBlogAdd,
    CompBlogDetail
  },
  methods:{
    chgComp (compName, data={}){
      // pushComp
      if(compName == this.currentView){
        this.currentView =''
        // TODO 强制刷新有点low
        setTimeout(()=>{
          this.replaceComp(compName, data)
        })
      } else{
        this.pushComp(compName, data)
      }
    },
    pushComp(compName, data={}){
      if(this.history.length -1 != this.currentIndex){
        this.history.splice(this.currentIndex+1)
      }
      this.history.push({compName, data})
      this.currentView = compName
      this.currentIndex = this.history.length-1
      this.data= data
    },
    go(n=0){
      let len = this.history.length
      let newIndex = this.currentIndex+n
      if(newIndex<len && newIndex>=0){
        this.currentView = this.history[newIndex].compName
        this.currentIndex = newIndex
      }
    },
    back(){
      this.go(-1)
    },
    forword(){
      this.go(1)
    },
    replaceComp(compName, data={}){
      this.history[this.currentIndex] = {compName, data}
      this.currentView = compName
      this.data= data
      this.data.__random =Math.random()
    }
  }

}
</script>
